package org.example;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test2 {
    private static final Logger logger = Logger.getLogger(Test2.class.getName());
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
            .withZone(ZoneId.systemDefault());

    public static void main(String[] args) {
        // 从命令行参数获取文件路径，默认路径为 "C:\\test\\example.txt"
        String filePathStr = args.length > 0 ? args[0] : "C:\\Users\\86158\\Desktop\\ob_整理文件测试\\day09-字符集、IO流（一）.md";
        Path filePath = Paths.get(filePathStr);

        try {
            if (!Files.exists(filePath)) {
                System.out.println("文件不存在: " + filePath);
                return;
            }

            // 获取文件基础属性
            BasicFileAttributes attrs = Files.readAttributes(filePath, BasicFileAttributes.class);

            // 获取时间戳（精确到纳秒）
            Instant creationTime = attrs.creationTime().toInstant();
            Instant lastModifiedTime = attrs.lastModifiedTime().toInstant();
            Instant lastAccessTime = attrs.lastAccessTime().toInstant();

            // 格式化并输出时间
            System.out.println("创建时间: " + FORMATTER.format(creationTime));
            System.out.println("修改时间: " + FORMATTER.format(lastModifiedTime));
            System.out.println("访问时间: " + FORMATTER.format(lastAccessTime));

        } catch (IOException e) {
            logger.log(Level.SEVERE, "读取文件属性时发生错误", e);
            System.out.println("读取文件属性时发生错误，请检查文件路径或权限");
        }
    }
}
